package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsNews;
import com.zjxf.base.bean.po.PmsAsProduct;
import com.zjxf.base.bean.po.PmsAsProductClassify;
import com.zjxf.base.bean.vo.VoCmsNews;
import com.zjxf.base.bean.vo.VoPmsAsProduct;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.CmsNewsService;
import com.zjxf.service.PmsAsProductClassifyService;
import com.zjxf.service.PmsAsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 首页内容管理Controller
 * Created by lgw on 2021/3/27.
 */
@Api(tags = "HomeController", description = "首页内容管理")
@AllArgsConstructor
@RestController
@RequestMapping("/app/home/")
public class HomeController extends BaseController {

    private final CmsNewsService cmsNewsService;

    private final PmsAsProductClassifyService pmsAsProductClassifyService;

    private final PmsAsProductService pmsAsProductService;

    /**
     * 获取商品分类列表
     *
     * @param parentId 分类的父id
     * @return JSONResponse
     */
    @ApiOperation("获取商品分类列表")
    @PostMapping(value = "productCateList/{parentId}")
    public JSONResponse getProductCateList(@NotNull @PathVariable Long parentId) {
        List<PmsAsProductClassify> pmsProductClassifyList = pmsAsProductClassifyService.findByPId(parentId);
        return success(pmsProductClassifyList);
    }

    /**
     * 获取所有新闻/轮播图列表
     *
     * @param filter type：0新闻/1轮播图  显示状态：0隐藏/1显示  条数限制
     * @return
     */
    @ApiOperation("获取新闻列表")
    @PostMapping(value = "newsList/getAll")
    public JSONResponse NewsPage(@Valid VoCmsNews.appFilter filter) {
        Page<CmsNews> cmsNewsPage = cmsNewsService.appPage(filter);
        JSONObject jsonData = cmsNewsService.pageRo(cmsNewsPage);
        return success(jsonData);
    }

    /**
     * 获取新闻详情
     *
     * @param id 新闻id
     * @return
     * @throws OperationException 数据不存在
     */
    @ApiOperation("获取新闻详情")
    @PostMapping(value = "news/newsDetail/{id}")
    public JSONResponse getNewsEntity(@NotNull @PathVariable Long id) throws OperationException {
        CmsNews cmsNews = cmsNewsService.getEntity(id);
        return success(cmsNews);
    }

    /**
     * 获取一乡一品与红军餐列表
     *
     * @param filter showLocation区分一乡一品与红军餐 publishStatus上架状态
     * @return
     */
    @ApiOperation(value = "分页获取一乡一品或红军餐列表")
    @PostMapping("products/getAll")
    public JSONResponse getAllOneVillage(@Valid VoPmsAsProduct.filter filter) {
        Page<PmsAsProduct> products = pmsAsProductService.page(filter);
        JSONObject jsonData = pmsAsProductService.sellerPageRo(products);
        return success(jsonData);
    }

    /**
     * 根据id获取商品详情
     *
     * @param id 商品id
     * @return
     * @throws OperationException 商品不存在
     */
    @ApiOperation(value = "获取商品详情")
    @PostMapping(value = "products/productDetail/{id}")
    public JSONResponse getOneVillageEntity(@NotNull @PathVariable Long id) throws OperationException {
        PmsAsProduct pmsAsProduct = pmsAsProductService.getEntity(id);
        return success(pmsAsProduct);
    }
}
